package com.league.zjxn.main.access

import androidx.core.widget.addTextChangedListener
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.AccessViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_access_register.*

class RegisterFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): RegisterFragment {
            return RegisterFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_register

    override fun initView() {

        etRegisterPhone.addTextChangedListener { editable ->
            editable?.let { viewModel.mMapRegister["mobile"] = it.toString() }
            tvRegisterSubmit.isEnabled =
                !editable.isNullOrEmpty() && !etRegisterCode.text.isNullOrEmpty()
                        && !etRegisterPwd1.text.isNullOrEmpty()
                        && !etRegisterPwd2.text.isNullOrEmpty()
        }
        etRegisterCode.addTextChangedListener { editable ->
            editable?.let { viewModel.mMapRegister["code"] = it.toString() }
            tvRegisterSubmit.isEnabled =
                !editable.isNullOrEmpty() && !etRegisterPhone.text.isNullOrEmpty()
                        && !etRegisterPwd1.text.isNullOrEmpty()
                        && !etRegisterPwd2.text.isNullOrEmpty()
        }
        etRegisterPwd1.addTextChangedListener { editable ->
            editable?.let { viewModel.mMapRegister["password"] = it.toString() }
            tvRegisterSubmit.isEnabled =
                !editable.isNullOrEmpty() && !etRegisterPhone.text.isNullOrEmpty()
                        && !etRegisterCode.text.isNullOrEmpty()
                        && !etRegisterPwd2.text.isNullOrEmpty()
        }
        etRegisterPwd2.addTextChangedListener { editable ->
            editable?.let { viewModel.mMapRegister["confirm_password"] = it.toString() }
            tvRegisterSubmit.isEnabled =
                !editable.isNullOrEmpty() && !etRegisterPhone.text.isNullOrEmpty()
                        && !etRegisterCode.text.isNullOrEmpty()
                        && !etRegisterPwd1.text.isNullOrEmpty()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        viewModel.mMapRegister.clear()
    }
}